﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using MagazineWebForms.Models;
using Newtonsoft.Json;

namespace MagazineWebForms
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
         
        }

        [WebMethod]
        public static string Authors()
        {
            var aut = BusinessMethodes.BusinessMethods.GetAuhtors();
            string json = JsonConvert.SerializeObject(aut);
            return json;
        }

        

         [WebMethod]
        public static  string GetListArticles(string sidx, string sord, int page, int rows)
        {
            AutModel aut = new AutModel();
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            var listType = BusinessMethodes.BusinessMethods.GetListAuhtors();
            var todolist = listType.Select(x => new AutModel
            {

                Id = x.Id,
               Name=x.Name,
               LastName=x.Lastname
            
            });


            int totalRecords = todolist.Count();
            var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
            if (sord.ToUpper() == "DESC")
            {
                todolist = todolist.OrderByDescending(s => s.Name);
                todolist = todolist.Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                todolist = todolist.OrderBy(s => s.Name);
                todolist = todolist.Skip(pageIndex * pageSize).Take(pageSize);
            }
            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = todolist
            };
          var Json=   JsonConvert.SerializeObject(jsonData);
            return Json;
        }


        
        
    }
}